//
//  MHXepHangViewController.m
//  MHO
//
//  Created by papo on 2/17/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHXepHangViewController.h"
#import "Contants.h"
#import "TTScrollSlidingPagesController.h"
#import "TTSlidingPage.h"
#import "TTSlidingPageTitle.h"
#import "MHAppDelegate.h"


@interface MHXepHangViewController (){
    NSArray*    viewControllers;
    BOOL firstAppRequest, firstGameRequest;
}
@property (strong, nonatomic) TTScrollSlidingPagesController *mySlider;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *topAppButton;
@property (weak, nonatomic) IBOutlet UIButton *topGameButton;
@property (strong, nonatomic) IBOutlet UITableViewController *topAppTableView;
@property (weak, nonatomic) IBOutlet UIView *topView;


@property (strong, nonatomic) IBOutlet UITableViewController *topGameTableView;
@end

@implementation MHXepHangViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.mostView = 1;
    self.group = kGroupGame;
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navibarView setTitle:nil leftButton:kFileBtnHuman rightButton:nil searchButton:YES];
    

//    viewControllers = @[];



    //initial setup of the TTScrollSlidingPagesController.
    self.mySlider = [[TTScrollSlidingPagesController alloc] init];

    //set properties to customiser the slider. Make sure you set these BEFORE you access any other properties on the slider, such as the view or the datasource. Best to do it immediately after calling the init method.
    //self.slider.hideStatusBarWhenScrolling = YES;
    self.mySlider.titleScrollerHidden = YES;
    //slider.titleScrollerHeight = 100;
    //slider.titleScrollerItemWidth=60;
    //slider.titleScrollerBackgroundColour = [UIColor darkGrayColor];
    //slider.disableTitleScrollerShadow = YES;
    self.mySlider.disableUIPageControl = YES;
    //slider.initialPageNumber = 1;
    //slider.pagingEnabled = NO;
    //slider.zoomOutAnimationDisabled = YES;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7){
        self.mySlider.hideStatusBarWhenScrolling = YES;//this property normally only makes sense on iOS7+. See the documentation in TTScrollSlidingPagesController.h. If you wanted to use it in iOS6 you'd have to make sure the status bar overlapped the TTScrollSlidingPagesController.
    }

    //set the datasource.
    self.mySlider.dataSource = self;
    self.mySlider.delegate = self;

    //add the slider's view to this view as a subview, and add the viewcontroller to this viewcontrollers child collection (so that it gets retained and stays in memory! And gets all relevant events in the view controller lifecycle)
    CGRect frame = self.view.frame;
    frame.origin.y += self.topView.frame.size.height + self.navibarView.frame.size.height;
    frame.size.height -= (self.topView.frame.size.height + self.navibarView.frame.size.height);
    self.mySlider.view.frame = frame;
    [self.view addSubview:self.mySlider.view];
    [self addChildViewController:self.mySlider];

    self.tableView = self.topGameTableView.tableView;

    [self refresh];

    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self performSelector:@selector(delayLoad) withObject:self afterDelay:0.1];
}

- (void)delayLoad {
    [self topGameAction:nil];
}

- (void)leftButtonPressed:(id)sender {
    [[MHAppDelegate sharedApp].viewController showLeftPanelAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)topAppAction:(id)sender {
    [self.mySlider scrollToPage:0 animated:YES];
    [self.topAppButton setEnabled:NO];
    [self.topGameButton setEnabled:YES];
    self.group = kGroupApp;
    self.tableView = self.topAppTableView.tableView;

    if (!firstAppRequest) {
//        firstAppRequest = YES;
        [self refresh];
    }
}

- (void)viewDidAppear:(BOOL)animated {
}


- (IBAction)topGameAction:(id)sender {
    [self.mySlider scrollToPage:1 animated:YES];
    [self.topAppButton setEnabled:YES];
    [self.topGameButton setEnabled:NO];

    self.group = kGroupGame;
    self.tableView = self.topGameTableView.tableView;

    if (!firstGameRequest) {
//        firstGameRequest = YES;
        [self refresh];
    }
}

#pragma mark TTSlidingPagesDataSource methods
-(NSInteger)numberOfPagesForSlidingPagesViewController:(TTScrollSlidingPagesController *)source{
    return 2;
}

-(TTSlidingPage *)pageForSlidingPagesViewController:(TTScrollSlidingPagesController*)source atIndex:(NSInteger)index{
    UIViewController *viewController;
    if (index == 0) {
        viewController = self.topAppTableView;
    }
    else {
        viewController = self.topGameTableView;
    }
    CGRect frame = self.mySlider.view.frame;
    frame.origin.y = 0;
    //    frame.size.height -= self.topView.frame.size.height;
    [viewController.view setFrame:frame];

    return [[TTSlidingPage alloc] initWithContentViewController:viewController];
}

-(TTSlidingPageTitle *)titleForSlidingPagesViewController:(TTScrollSlidingPagesController *)source atIndex:(NSInteger)index{
    TTSlidingPageTitle *title;
    if (index == 0){
        //use a image as the header for the first page
        title= [[TTSlidingPageTitle alloc] initWithHeaderText:@"Top App"];
    } else {
        //all other pages just use a simple text header
        title = [[TTSlidingPageTitle alloc] initWithHeaderText:@"Top Game"];

    }
    return title;
}

////The below method in the datasource might get removed from the control some time in the future as it doesn't work that well with the headers if the width is small.
//-(int)widthForPageOnSlidingPagesViewController:(TTScrollSlidingPagesController *)source atIndex:(int)index
//{
//    if (index ==3){
//        return 130;
//    } else {
//        return self.view.frame.size.width;
//    }
//}

#pragma mark - delegate
-(void)didScrollToViewAtIndex:(NSUInteger)index
{
    if (index == 0) {
//        self.tableView = self.topAppTableView.tableView;
//        self.group = kGroupApp;
        [self topAppAction:nil];
    }
    else {
//        self.tableView = self.topGameTableView.tableView;
//        self.group = kGroupGame;
        [self topGameAction:nil];
    }
//    [self refresh];
}

@end
